package light.mvc.service.patient.impl;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TimeZone;

import light.mvc.dao.BaseDaoI;
import light.mvc.model.admin.Tuser;
import light.mvc.model.register.Tregister;
import light.mvc.page.patient.DoctorRegistration;
import light.mvc.page.patient.Registration;
import light.mvc.service.patient.ReserveRegisterServiceI;
import light.mvc.utils.GlobalConstant;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;

@Service
public class ReserveRegisterServiceImpl implements ReserveRegisterServiceI {

	@Autowired
	private BaseDaoI<Tuser> doctorDao;
	
	@Autowired
	private BaseDaoI<Tregister> registerDao;
	
	@Autowired
	private BaseDaoI<Tuser> userDao;
	

	
	@Override
	public void add(Integer patientId,Integer doctorId,Long timestamp) throws Exception {
		Date timeSegment = new Date(timestamp);		//预约的时间段
//		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

		Map<String,Object> params = new HashMap<String,Object>();
		params.put("doctorId", doctorId);
		params.put("timeSegment", timeSegment);

		String hql = "select count(*) from Tregister t join t.doctor doctor where t.type = 0 and doctor.id = :doctorId and t.timeSegment = :timeSegment";
		Long registeredNum = registerDao.count(hql,params);
		if(registeredNum >= GlobalConstant.MAX_RESERVED_REGISTED) {
			throw new Exception("该时间段剩余预约挂号数为0");
		}
		
		params.remove("doctorId");
		params.put("patientId", patientId);
		hql = "select count(*) from Tregister t join t.doctor doctor join t.patient patient where t.type = 0" +
					" and t.timeSegment = :timeSegment and patient.id = :patientId";
		Long id = registerDao.count(hql,params);
		if(id != 0) {
			throw new Exception("您已预约了该时间段的挂号");
		}
		
		Tregister t = new Tregister();
		t.setType(0);  					//预约挂号
		t.setReserveTime(new Date());	//预约挂号时的时间
		t.setTimeSegment(timeSegment);
		t.setPatient(userDao.get(Tuser.class, patientId));
		t.setDoctor(userDao.get(Tuser.class, doctorId));
		
		t.setId((Integer)registerDao.save(t));
	}
	
	@Override
	public DoctorRegistration detail(Integer doctorId) {
		Tuser t = doctorDao.get(Tuser.class,doctorId);
		
		DoctorRegistration doctorRegistration = new DoctorRegistration();
		doctorRegistration.setDoctorId(t.getId());
		doctorRegistration.setDoctorName(t.getName());
		doctorRegistration.setDepartmentId(t.getDepartment().getId());
		doctorRegistration.setDepartmentName(t.getDepartment().getName());
		
		int i=1;
		int addDay=1;
		List<Registration> registrations = new ArrayList<Registration>();
		while(i <= 5) {
			Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("GMT+08:00"));    //获取东八区时间
			calendar.add(Calendar.DATE, addDay++);
			
			int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
			if(dayOfWeek < 2 || dayOfWeek > 6) {
				continue;
			}
			
			calendar.set(Calendar.HOUR_OF_DAY, 8);
			calendar.set(Calendar.MINUTE, 0);
			calendar.set(Calendar.SECOND, 0);
			Registration registration = getRegistrationByCalendar(calendar,t.getId());
			registrations.add(registration);
			
			calendar.set(Calendar.HOUR_OF_DAY, 14);
			calendar.set(Calendar.MINUTE, 0);
			calendar.set(Calendar.SECOND, 0);
			registration = getRegistrationByCalendar(calendar,t.getId());
			registrations.add(registration);
			
			i++;
		}
		doctorRegistration.setRegistrations(JSONObject.toJSONString(registrations));
//		doctorRegistration.setRegistrations(registrations);
		
		return doctorRegistration;
	}
	
	
	private Registration getRegistrationByCalendar(Calendar calendar,Integer doctorId) {
		Registration registration = new Registration();
		registration.setTimeSegment(calendar.getTime());
		
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("doctorId", doctorId);
		params.put("timeSegment", calendar.getTime());
		
		String hql = "select count(*) from Tregister t join t.doctor doctor where t.type = 0 and doctor.id = :doctorId and t.timeSegment = :timeSegment";
		Long registeredNum = registerDao.count(hql,params);
		registration.setReserveRegisteredNum(registeredNum);
		registration.setReserveRegisterFreeNum(GlobalConstant.MAX_RESERVED_REGISTED-registeredNum);
		
		hql = "select count(*) from Tregister t join t.doctor doctor where t.type = 1 and doctor.id = :doctorId and t.timeSegment = :timeSegment";
		registeredNum = registerDao.count(hql,params);
		registration.setSiteRegisteredNum(registeredNum);
		registration.setSiteRegisterFreeNum(GlobalConstant.MAX_SITE_REGISTED-registeredNum);
		
		return registration;
	}
	
	
}
